﻿// --------------------------------
// <copyright file="ControlConfig.cs" company="Faculty of Information Technology, Czech Technical University in Prague">
//     This document is shared by Microsoft Public License (MS-PL)
//     For further details please see http://msdn.microsoft.com/en-us/library/ff648068.aspx or license.txt.
// </copyright>
// <author>Lukáš Molnár</author>

// <email>molnaluk@fit.cvut.cz</email>

// <date>9. 11. 2011 22:22:36</date>

// <summary>
//
// </summary>
// --------------------------------

using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using Microsoft.Xna.Framework.Input;

namespace FITClub.Episode1.Controller
{
    /// <summary>
    /// Configuration for keyboard
    /// </summary>
    public class ControlConfig : List<Keys>
    {
        public static Keys[] DefaultKeySet = { Keys.Up, Keys.Left, Keys.Down, Keys.Right, Keys.RightControl, Keys.RightShift, Keys.Back };

        public string PlayerName { get; set; }

        [XmlIgnore]
        public Keys Up
        {
            get { return this[0]; }
            set { this.InsertKey(0, value); }
        }

        [XmlIgnore]
        public Keys Down
        {
            get { return this[2]; }
            set { this.InsertKey(2, value); }
        }

        [XmlIgnore]
        public Keys Left
        {
            get { return this[1]; }
            set { this.InsertKey(1, value); }
        }

        [XmlIgnore]
        public Keys Right
        {
            get { return this[3]; }
            set { this.InsertKey(3, value); }
        }

        [XmlIgnore]
        public Keys Attack
        {
            get { return this[4]; }
            set { this.InsertKey(4, value); }
        }

        [XmlIgnore]
        public Keys Defence
        {
            get { return this[5]; }
            set { this.InsertKey(5, value); }
        }

        [XmlIgnore]
        public Keys Reset
        {
            get { return this[6]; }
            set { this.InsertKey(6, value); }
        }

        private void InsertKey(int pos, Keys keys)
        {
            int i = this.IndexOf(keys);
            if (i >= 0 && pos != i)
            {
                if (keys == this[pos])
                {
                    this[i] = Keys.None;
                }
                else
                {
                    this[i] = this[pos];
                }
            }
            this[pos] = keys;
        }

        public ControlConfig()
        {
        }

        public ControlConfig(Keys[] keys)
            : base(keys)
        {
        }

        public bool Intersects(ControlConfig config)
        {
            foreach (var item in this)
            {
                if (config.Contains(item))
                {
                    return true;
                }
            }
            return false;
        }

        private static string Filename(string username)
        {
            return string.Format("{0}.controls", username);
        }

        public void Save(string username)
        {
            XmlSerializer xser = new XmlSerializer(this.GetType());

#if WINDOWS
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain())
#endif
#if XBOX || WINDOWS_PHONE
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())            //.GetUserStoreForDomain()
#endif
            using (IsolatedStorageFileStream stream = storage.OpenFile(Filename(username), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
            {
                xser.Serialize(stream, this);
            }
        }

        public static ControlConfig Load(string username)
        {
            XmlSerializer xser = new XmlSerializer(typeof(ControlConfig));
#if WINDOWS
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain())
#endif
#if XBOX || WINDOWS_PHONE
            using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())            //.GetUserStoreForDomain()
#endif
            using (IsolatedStorageFileStream stream = storage.OpenFile(Filename(username), System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                return xser.Deserialize(stream) as ControlConfig;
            }
        }
    }
}